Skip to content

feat: add user subcommands for account recovery - #1095

Merged
jmattheis merged 4 commits into
gotify:masterfrom
Shrinet82:cli-user-recovery
Dec 4, 2026
Merged

jmattheis merged 4 commits into
gotify:masterfrom
Shrinet82:cli-user-recovery

Conversation

@Shrinet82

@Shrinet82 Shrinet82 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1043

This adds the user subcommands that @jmattheis proposed in the issue, so a lost admin account can be recovered without access to the API:

gotify user create [--admin] <name> [password]
gotify user set-password <name> [password]
gotify user set-admin <name> true|false

If [password] is omitted, it is prompted for twice without echo. If stdin isn't a terminal, the command exits with an error instead of hanging. Passwords go through the same ValidateNewPassword/CreatePassword (with GOTIFY_PASSSTRENGTH) as the API. The commands load the config exactly like serve (env vars and env files), so they hit the same database.

Example, recovering the admin in Docker:

$ docker exec -it gotify ./gotify-app user set-password admin
Password:
Confirm password:
Updated password of user "admin"

Some notes:

  • I open the database with createDefaultUserIfNotExist=false, so running a command against an empty database doesn't also create admin:admin. It only makes the requested change.
  • set-admin <name> false currently follows the spec and doesn't stop you from demoting the last admin, while the API refuses that ("cannot delete last admin"). I can add the same check here if you'd prefer. You could always promote a user again with the CLI.
  • Usage errors exit with 2, other errors (user exists, user not found, password mismatch, ...) with 1.
  • This adds golang.org/x/term for the password prompt, at the version x/crypto already requires.

I tested on a clean Ubuntu 24.04 VM with Go 1.27.4:

  • make test-coverage and make check-go (golangci-lint v2.15.0)
  • SQLite end to end with the binary: started and stopped serve, ran create (argument and real terminal prompt), create --admin, set-password admin and set-admin alice true, then checked over the API that the old admin password returns 401, the new one 200, and alice is admin. Also checked all the error cases and their exit codes.
  • Docker: built the image from this branch with docker/Dockerfile and recovered the admin with docker exec -it gotify ./gotify-app user set-password admin.
  • Postgres 17 in a container: set-password, create and set-admin, then verified via the API.

Docs: gotify/website#110 describes how to reset a lost admin password with these commands.

Add `gotify user create [--admin] <name> [password]`,
`gotify user set-password <name> [password]` and
`gotify user set-admin <name> true|false`. They load the same config
as `gotify serve` and operate directly on the configured database,
so a lost admin account can be recovered without API access.

If the password is omitted, it is prompted for twice without echo.
If stdin is not a terminal, the command fails instead of hanging.
Passwords are validated and hashed like in the API.

The commands never create the default user, so on an empty database
only the requested change is made.

Fixes gotify#1043
@Shrinet82

Copy link
Copy Markdown
Contributor Author

@jmattheis a gentle ping. I rebased this onto current master today, so it includes the elevate change. The build workflow is waiting for a maintainer to approve the run since this is my first PR here. Could you approve it and take a look when you have a moment? The docs side is in gotify/website#110.

@jmattheis

Copy link
Copy Markdown
Member

Yeah, it's on my todo list, but in the last two weeks I had not much time for open source and there is quite a lot in my backlog. On first glance it looks okay.

@Shrinet82

Copy link
Copy Markdown
Contributor Author

totally understandable! take your time

@jmattheis jmattheis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@jmattheis
jmattheis added this pull request to the merge queue Dec 4, 2026
@codecov

codecov Bot commented Dec 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.45%. Comparing base (2affea1) to head (ccec193).
⚠️ Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
user.go 87.90% 15 Missing ⚠️
app.go 83.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1095      +/-   ##
==========================================
+ Coverage   81.82%   82.45%   +0.62%     
==========================================
  Files          69       70       +1     
  Lines        3290     3419     +129     
==========================================
+ Hits         2692     2819     +127     
- Misses        598      600       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Merged via the queue into gotify:master with commit b9fa7ba Dec 4, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Allow account recovery without access to an admin account

2 participants